package com.gc.controller.textbookServlet;



import com.gc.entity.TextBook;
import com.gc.repository.TextBookRepository;
import com.gc.repository.impl.TextBookRepositoryImpl;
import com.gc.util.result.R;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet(name = "textbookAdd", value = "/textbookAdd")
public class textbookAdd extends HttpServlet {
    TextBookRepository textbookRepository = new TextBookRepositoryImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();

        /**
         *     long id;
         *     String name;
         *     String tid;
         *     String sex;
         *     String age;
         *     String password;
         *     String phone;
         *     String email;
         */
        boolean res = textbookRepository.Add(new TextBook(
                null,
                request.getParameter("bookid"),
                request.getParameter("name"),
                request.getParameter("ISBN"),
                request.getParameter("publisher"),
                new BigDecimal(request.getParameter("price"))));

        if(res) {
            response.getWriter().write(gson.toJson(R.ok().data("ok")));
        } else {
            response.getWriter().write(gson.toJson(R.error()));
        }


    }
}
